package com.itzixi.mcp.tool;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itzixi.mapper.AdminUserMapper;
import com.itzixi.pojo.AdminUserDO;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName UserTool
 * @Author 全栈工程师
 * @Version 1.0
 * @Description UserTool
 **/
@Component
@Slf4j
public class UserTool {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CreateUserRequest {
        @ToolParam(description = "用户账号")
        private String username;
        @ToolParam(description = "用户昵称")
        private String nickname;
        @ToolParam(description = "备注（可以为空）")
        private String remark;
        @ToolParam(description = "用户邮箱")
        private String email;
        @ToolParam(description = "手机号码")
        private String mobile;
        @ToolParam(description = "用户性别（0:女 1:男 2:未知）")
        private Integer sex;
        @ToolParam(description = "工号")
        private String workNumber;
    }

    @Tool(description = "创建/新增用户信息记录")
    public String createNewUser(CreateUserRequest createUserRequest) {

        log.info("========== 调用MCP工具：createNewUser() ==========");
        log.info(String.format("| 参数 createUserRequest 为： %s", createUserRequest.toString()));
        log.info("========== End ==========");

        AdminUserDO user = new AdminUserDO();
        BeanUtils.copyProperties(createUserRequest, user);

        // 生成随机用户ID
        user.setId(RandomStringUtils.randomAlphanumeric(12));

        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        adminUserMapper.insert(user);

        return "用户信息创建成功";
    }

    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public static class QueryUserRequest {

        @ToolParam(description = "用户的ID", required = false)
        private String id;
        @ToolParam(description = "用户账号", required = false)
        private String username;
        @ToolParam(description = "用户昵称", required = false)
        private String nickname;
        @ToolParam(description = "手机号码", required = false)
        private String mobile;
        @ToolParam(description = "工号", required = false)
        private String workNumber;
    }

    @Tool(description = "根据条件查询用户信息")
    public List<AdminUserDO> queryUserListByCondition(QueryUserRequest queryUserRequest) {

        log.info("========== 调用MCP工具：queryUserListByCondition() ==========");
        log.info(String.format("| 参数 queryUserRequest 为： %s", queryUserRequest.toString()));
        log.info("========== End ==========");

        String id = queryUserRequest.getId();
        String username = queryUserRequest.getUsername();
        String nickname = queryUserRequest.getNickname();
        String mobile = queryUserRequest.getMobile();
        String workNumber = queryUserRequest.getWorkNumber();

        QueryWrapper<AdminUserDO> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(id)) {
            queryWrapper.eq("id", id);
        }
        if (StringUtils.isNotBlank(username)) {
            queryWrapper.like("username", username);
        }
        if (StringUtils.isNotBlank(nickname)) {
            queryWrapper.like("nickname", nickname);
        }
        if (StringUtils.isNotBlank(mobile)) {
            queryWrapper.like("mobile", mobile);
        }
        if (StringUtils.isNotBlank(workNumber)) {
            queryWrapper.like("work_number", workNumber);
        }

        List<AdminUserDO> userList = adminUserMapper.selectList(queryWrapper);

        return userList;
    }
}